package com.zws.cucumber.optimization;

import com.zws.cucumber.optimization.socket.ZwsSockets;

import java.io.IOException;

public class ZwsOptimizations {

    public static final String Prop_Socket_EnableMemorySocket = "cucumber-ct.socket.enableMemorySocket";
    public static final String Prop_Jetty_EnableExecutionInMainThread = "cucumber-ct.jetty.enableExecutionInMainThread";
    public static final String Prop_Step_EnableInitJustOneTime = "cucumber-ct.step.enableInitJustOneTime";

    private static boolean Flag_Socket_MemorySocket = false;
    private static boolean Flag_Jetty_ExecutionInMainThread = false;
//    private static boolean Flag_Step_EnableInitJustOneTime = false;

    public static void enableMemorySocketIfConfigured() throws IOException {
        if (!ZwsSockets.isInit()) {
            Flag_Socket_MemorySocket = Boolean.parseBoolean(System.getProperty(ZwsOptimizations.Prop_Socket_EnableMemorySocket));
            if (Flag_Socket_MemorySocket) {
                ZwsSockets.init();
            }
        }
    }

    public static void enableJettyExecutionInMainThreadIfConfigured() {
        Flag_Jetty_ExecutionInMainThread = Boolean.parseBoolean(System.getProperty(Prop_Jetty_EnableExecutionInMainThread, "false"));
        if (Flag_Jetty_ExecutionInMainThread) {
            if (!Flag_Socket_MemorySocket) {
                throw new IllegalArgumentException("set cucumber-ct.jetty.enableExecutionInMainThread to true if and only if cucumber-ct.socket.enableMemorySocket is true");
            }
        }
    }

//    public static void enableStepInitJustOneTimeIfConfigured() {
//        Flag_Step_EnableInitJustOneTime = Boolean.parseBoolean(System.getProperty(Prop_Step_EnableInitJustOneTime, "false"));
//    }

    public static boolean isSocketMemorySocketEnabled() {
        return Flag_Socket_MemorySocket;
    }

    public static boolean isJettyExecutionInMainThreadEnabled() {
        return Flag_Jetty_ExecutionInMainThread;
    }

    public static boolean isStepInitJustOneTimeEnabled() {
        return Boolean.parseBoolean(System.getProperty(Prop_Step_EnableInitJustOneTime, "false"));
    }

}
